package com.example.book_recommend.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.book_recommend.entity.BookInfo;
import com.example.book_recommend.entity.Store;
import com.example.book_recommend.entity.SysToken;
import com.example.book_recommend.pojo.VueResultInfo;
import com.example.book_recommend.service.*;
import com.example.book_recommend.utils.ResultCode;
import com.example.book_recommend.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

@RestController
@EnableAutoConfiguration
@CrossOrigin
@RequestMapping("api/storeInfo")
public class StoreInfoController {
    @Autowired
    BookService bookService;

    @Autowired
    TokenService tokenService;

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    RoleService roleService;

    @Autowired
    RolePrivilegeService rolePrivilegeService;

    @Autowired
    StoreService storeService;

    /**
     * 图书上架；
     */
    @RequestMapping("upBook")
    public VueResultInfo upBook(@RequestBody BookInfo bookInfo, @RequestHeader("token") String token){
        SysToken sysToken = tokenService.findToken(token);
        if(sysToken == null || token == null){
            return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:尚未登录", null);
        }
        Integer uid = sysToken.getUserId();
        Integer[] roles =  userRoleService.getRolesByUser(uid);

        if(!Arrays.asList(roles).contains(31)){
            return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:身份错误", null);
        }
        Store store = storeService.getByManagerId(uid);
        if(store == null){
            return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:您只能访问自己的商店", null);
        }

        bookInfo.setStoreId(store.getStoreId());
        bookInfo.setBookId(null);
        bookInfo.setPublishDate(String.valueOf(new Date()));
//        bookInfo.setBookCategoryId();
        bookInfo.setDealMount(0);
        bookInfo.setLookMount(0);
        int c = bookService.insert(bookInfo);

        return ResultFactory.buildSuccessResult("影响的行数：" + String.valueOf(c));

    }

    /**
     * 图书下架；
     */
    @RequestMapping("downBook")
    public VueResultInfo downBook(@RequestBody JSONObject jsonObject, @RequestHeader("token") String token){
        SysToken sysToken = tokenService.findToken(token);
        if(sysToken == null || token == null){
            return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:尚未登录", null);
        }
        Integer uid = sysToken.getUserId();
        Integer[] roles =  userRoleService.getRolesByUser(uid);

        if(!Arrays.asList(roles).contains(31) && !Arrays.asList(roles).contains(0)){
            return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:身份错误", null);
        }

        Integer storeId;
        String _s = String.valueOf(jsonObject.get("bookId"));
        if(_s == null || _s.equals("")){
            return ResultFactory.buildFailResult("参数错误：bookId不能为空！");
        }
        System.out.println(_s);

        Long bookId = Long.valueOf(_s);
        BookInfo bookInfo = null;
        System.out.println(bookId);

        bookInfo = bookService.getBookInfo(bookId);
        if(bookInfo == null){
            return ResultFactory.buildFailResult("参数错误：指定bookId未找到！");
        }
        if(Arrays.asList(roles).contains(31)){   //说明是商家，通过查表获取商铺id
            storeId = storeService.getByManagerId(uid).getStoreId();
            if(!bookInfo.getStoreId().equals(storeId)){
                return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:只能访问自己的商铺", null);
            }
        }

        bookInfo.setIsShelf(0);
        return ResultFactory.buildSuccessResult("影响的行数：" + bookService.update(bookInfo));
    }

    /**
     * 图书信息修改；
     */
    @RequestMapping("modifyBook")
    public VueResultInfo modifyBook(@RequestBody BookInfo bookInfo0, @RequestHeader("token") String token){
        SysToken sysToken = tokenService.findToken(token);
        if(sysToken == null || token == null){
            return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:尚未登录", null);
        }
        Integer uid = sysToken.getUserId();
        Integer[] roles =  userRoleService.getRolesByUser(uid);

        if(!Arrays.asList(roles).contains(31) && !Arrays.asList(roles).contains(0)){
            return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:身份错误", null);
        }

        Integer storeId;
        Long bookId = bookInfo0.getBookId();
        BookInfo bookInfo = null;

        if(bookId == null){
            return ResultFactory.buildFailResult("参数错误：bookId不能为空！");
        }
        bookInfo = bookService.getBookInfo(bookId);
        if(Arrays.asList(roles).contains(31)){   //说明是商家，通过查表获取商铺id
            storeId = storeService.getByManagerId(uid).getStoreId();
            if(!bookInfo.getStoreId().equals(storeId)){
                return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:只能访问自己的商铺", null);
            }
        }

        bookInfo0.setStoreId(null); //禁止在商铺间转移
        return ResultFactory.buildSuccessResult("影响的行数：" + bookService.update(bookInfo0));
    }

    /**
     * 查看指定店铺的所有图书；
     */
    @RequestMapping("getBooksOfStore")
    public VueResultInfo getBooksOfStore(@RequestBody JSONObject jsonObject, @RequestHeader("token") String token){
        SysToken sysToken = tokenService.findToken(token);
        if(sysToken == null || token == null){
            return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:尚未登录", null);
        }
        Integer uid = sysToken.getUserId();
//        Integer[] roles =  userRoleService.getRolesByUser(uid);

        Integer page = (Integer) jsonObject.get("page") == null ? 0 : (Integer) jsonObject.get("page");
        Integer size = (Integer) jsonObject.get("size") == null ? 50: (Integer) jsonObject.get("size");
        Integer isShelf = (Integer) jsonObject.get("isShelf") == null ? 1: (Integer) jsonObject.get("isShelf");
        Integer storeId = (Integer) jsonObject.get("storeId");

        if(storeId == null){
            return ResultFactory.buildFailResult("storeId 不能为空");
        }

        return ResultFactory.buildSuccessResult(bookService.getBookByPage(page, size, storeId, isShelf));
    }

    /**
     * 查看指定店铺图书总数；
     */
    @RequestMapping("getBooksPageOfStore")
    public VueResultInfo getBooksPageOfStore(@RequestBody JSONObject jsonObject, @RequestHeader("token") String token){
        SysToken sysToken = tokenService.findToken(token);
        if(sysToken == null || token == null){
            return ResultFactory.buidResult(ResultCode.UNAUTHORIZED, "无权访问:尚未登录", null);
        }
        Integer uid = sysToken.getUserId();
//        Integer[] roles =  userRoleService.getRolesByUser(uid);

        Integer storeId = (Integer) jsonObject.get("storeId");

        if(storeId == null){
            return ResultFactory.buildFailResult("storeId 不能为空");
        }

        return ResultFactory.buildSuccessResult(bookService.getBookPageSizeByPage(storeId));
    }
}
